<template>
  <div>
    <ul>
      <li v-for="message in messageList" :key="message.id">
        <!-- 跳转路由并携带query参数，to的字符串写法 -->
        <!-- <router-link
          :to="`/home/message/detail?id=${message.id}&title=${message.title}`"
          >{{ message.title }}</router-link
        > -->
        <!-- 第二种方法使用对象的方法 -->
        <!-- <router-link
          :to="{
            path: '/home/message/detail',
            query: {
              id: message.id,
              title: message.title,
            },
          }"
          >{{ message.title }}</router-link
        > -->
        <!-- 跳转路由并携带query参数,to对象写法 -->
        <router-link
          :to="{
            name: 'mxiangqing',
            //在 route 的query里面
            query: {
              id: message.id,
              title: message.title,
            },
          }"
          >{{ message.title }}
          <button @click="pushShow(message)">push查看</button>
          <button @click="replaceShow(message)">replace查看</button>
        </router-link>
      </li>
    </ul>
    <br />
    <br />
    <div><router-view></router-view></div>
  </div>
</template>

<script>
export default {
  name: "Message",
  data() {
    return {
      messageList: [
        { id: "001", title: "消息001" },
        { id: "002", title: "消息002" },
        { id: "003", title: "消息003" },
      ],
    };
  },
  mounted() {
    console.log("message 实例被创建了");
  },
  methods: {
    pushShow(message) {
      this.$router.push({
        name: "mxiangqing",
        params: {
          id: message.id,
          title: message.title,
        },
      });
    },
    replaceShow(message) {
      this.$router.replace({
        name: "mxiangqing",
        params: {
          id: message.id,
          title: message.title,
        },
      });
    },
  },
};
</script>